//沉没孤岛 --- 跟上面求孤岛的总面积一样、
// 先标记出孤岛，再遍历恢复原靠近陆地的岛屿
// 靠近陆地的岛屿置为2，孤岛后续置为0，再将2恢复为1

const rl = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

let input = [];
let [n, m] = [0, 0];
let grid = [];
let dx = [0, 0, 1, -1];
let dy = [1, -1, 0, 0];

rl.on('line', (line) => {
  input.push(line.split(' '));
})
.on('close', () => {
  processInput(input);

  // 为了得到孤岛，我们只用遍历边界
  // 上下
  for (i = 0; i < m; i++) {
    if (grid[0][i] === 1) {
      dfs([0, i]);
    }
    if(grid[n-1][i] === 1) {
      dfs([n-1, i]);
    }
  }
  // 左右
  for (i = 0; i < n; i++) {
    if (grid[i][0] === 1) {
      dfs([i, 0]);
    }
    if(grid[i][m-1] === 1) {
      dfs([i, m-1]);
    }
  }

  chenMoGuDao();
  processOutput();

})

function processInput(input) {
  [n, m] = input[0].map(Number);
  grid = input.slice(1).map(row => row.map(Number));
}

function dfs(start) {
  let [x, y] = start;
  
  // 将它置为2，表示已经访问过，后续还要恢复
  grid[x][y] = 2;

  for (let i = 0; i < 4; i++) {
    let [nx, ny] = [x + dx[i], y + dy[i]];
    if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] === 1) {
      dfs([nx, ny]);
    }
  }
}

// 恢复原靠近陆地的岛屿 并将孤岛置为0沉默
function chenMoGuDao() {
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < m; j++) {
      if (grid[i][j] === 1) {
        grid[i][j] = 0;
      } else if (grid[i][j] === 2) {
        grid[i][j] = 1;
      }
    }
  }
}

function processOutput() {
  grid.forEach(row => console.log(row.join(' ')));
}